package com.liuhe.collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Demo1 {
    public static void main(String[] args) {
        //collections 工具类的使用
        ArrayList<String> list = new ArrayList<>();
        //批量添加
        Collections.addAll(list, "AAA", "BBBc", "bbbbccc");
        System.out.println(list);
        //打乱顺序
        Collections.shuffle(list);
        System.out.println(list);
        //排序
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.length() - o2.length();
            }
        });
        System.out.println(list);
        //二分查找元素
        int index = Collections.binarySearch(list, "AAA");
        System.out.println(index);

        //使用制定元素填充集合
//        Collections.fill(list,"jack");
        System.out.println(list);
        //拷贝集合中的元素
        ArrayList<String> list2 = new ArrayList<>();
        Collections.addAll(list2,"c","c","c");
        Collections.copy(list2,list);
        System.out.println(list2);
        //获取最大值和最小值
        String max = Collections.max(list);
        System.out.println(max);
        //交换元素
        Collections.swap(list,1,2);
        System.out.println(list);

    }
}
